package com.yuju.controller.order;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yuju.common.PageModel;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.order.enums.InvoiceReturnCodeEnum;
import com.yuju.order.model.InvoiceModel;
import com.yuju.order.service.IInvoiceService;
import com.yuju.util.Utils;
import com.yuju.vo.order.InvoiceVo;
/**
 * 
 * 类名：RoomOrderGuestController  <br />
 *
 * 功能：
 *
 * @author : sunyl <br />
 * @Date : 2017年11月25日 下午5:53:17  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("invoice")
public class InvoiceController extends BaseController<InvoiceVo, InvoiceModel> {
	@Autowired
	private IInvoiceService invoiceService;
	/**
	 * 新增修改发票
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2017年12月27日 下午2:41:50 <br/>
	 */
	@RequestMapping("edit")
	@ResponseBody
	public Result edit(@RequestBody JSONObject jsonObject) {
		InvoiceVo InvoiceVo = jsonObject.toJavaObject(InvoiceVo.class);
		String empId = jsonObject.getString("empId");
		String empName = jsonObject.getString("empName");
		InvoiceVo.setUpdateUserId(empId);
		InvoiceVo.setUpdateUserName(empName);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(InvoiceVo, "save", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = 0;
		if(InvoiceVo.getId()!= null){
			rows = invoiceService.updateInvoice(convert(InvoiceVo));
		}else{
			InvoiceVo.setCreateUserId(empId);
			InvoiceVo.setCreateUserName(empName);
			rows = invoiceService.save(convert(InvoiceVo));
			
		}
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(InvoiceReturnCodeEnum.INVOICE_SAVE_SUCCESS.getCode());
			result.setReturnMsg(InvoiceReturnCodeEnum.INVOICE_SAVE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(InvoiceReturnCodeEnum.INVOICE_SAVE_FAILURE.getCode());
			result.setReturnMsg(InvoiceReturnCodeEnum.INVOICE_SAVE_FAILURE.getDesc());
		}
		return result;
	}
	@RequestMapping("list")
	@ResponseBody
	public Result list(@RequestBody JSONObject jsonObject)   {
		InvoiceVo InvoiceVo = jsonObject.toJavaObject(InvoiceVo.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(InvoiceVo, "pager", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		PageModel pageModel = invoiceService.pageList(convert(InvoiceVo), InvoiceVo.getPage(), InvoiceVo.getRows());
		if (Utils.isNotNull(pageModel) && Utils.isNotNull(pageModel.getRows())) {
			result.setTotal(pageModel.getTotal());
			result.setRows(pageModel.getRows());
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(InvoiceReturnCodeEnum.INVOICE_QUERY_SUCCESS.getCode());
			result.setReturnMsg(InvoiceReturnCodeEnum.INVOICE_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	/**
	 * 单纯列表 不分页
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2017年12月19日 上午11:34:36 <br/>
	 */
	@RequestMapping("listPure")
	@ResponseBody
	public Result listPure(@RequestBody JSONObject jsonObject)   {
		
		InvoiceVo InvoiceVo = jsonObject.toJavaObject(InvoiceVo.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(InvoiceVo, "roomOrderId", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		List<InvoiceModel> roomorderGuestList = invoiceService.list(convert(InvoiceVo));
		if (Utils.isNotNull(roomorderGuestList) && roomorderGuestList.size()>0) {
			result.setData(roomorderGuestList);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(InvoiceReturnCodeEnum.INVOICE_QUERY_SUCCESS.getCode());
			result.setReturnMsg(InvoiceReturnCodeEnum.INVOICE_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	@RequestMapping("update")
	@ResponseBody
	public Result update(@RequestBody JSONObject jsonObject) {
		InvoiceVo InvoiceVo = jsonObject.toJavaObject(InvoiceVo.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(InvoiceVo, "update", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = invoiceService.updateSelective(convert(InvoiceVo));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(InvoiceReturnCodeEnum.INVOICE_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(InvoiceReturnCodeEnum.INVOICE_UPDATE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(InvoiceReturnCodeEnum.INVOICE_UPDATE_FAILURE.getCode());
			result.setReturnMsg(InvoiceReturnCodeEnum.INVOICE_UPDATE_FAILURE.getDesc());
		}
		return result;
	}

	@RequestMapping("delete")
	@ResponseBody
	public Result delete(@RequestBody JSONObject jsonObject) {
		InvoiceVo InvoiceVo = jsonObject.toJavaObject(InvoiceVo.class);
		// 参数校验
				Map<String, String> fail = FieldValidator.validateAll(InvoiceVo, "del", true);
				if (!fail.isEmpty()) {
					return validateError(fail);
				}
		Result result = new Result();
		int rows = invoiceService.del(InvoiceVo.getId());
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(InvoiceReturnCodeEnum.INVOICE_DEL_SUCCESS.getCode());
			result.setReturnMsg(InvoiceReturnCodeEnum.INVOICE_DEL_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(InvoiceReturnCodeEnum.INVOICE_DEL_FAILURE.getCode());
			result.setReturnMsg(InvoiceReturnCodeEnum.INVOICE_DEL_FAILURE.getDesc());
		}
		return result;
	}

	@RequestMapping("get")
	@ResponseBody
	public Result get(@RequestBody JSONObject jsonObject) {
		InvoiceVo InvoiceVo = jsonObject.toJavaObject(InvoiceVo.class);
		Result result = new Result();
		InvoiceModel GroupModel = invoiceService.find(convert(InvoiceVo));
		if (Utils.isNotNull(GroupModel)) {
			result.setData(GroupModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(InvoiceReturnCodeEnum.INVOICE_QUERY_SUCCESS.getCode());
			result.setReturnMsg(InvoiceReturnCodeEnum.INVOICE_QUERY_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
}
